package org.fireflow2.designer.eclipse.parts.basic;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.fireflow2.designer.eclipse.figures.CommonFigureInterface;
import org.fireflow2.designer.eclipse.figures.RouterFigure;
import org.fireflow2.designer.eclipse.figures.widget.RouterAnchor;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.RouterWrapper;

public class BasicRouterPart extends AbstractGraphicalEditPart implements
		NodeEditPart, PropertyChangeListener {
	private List leavingConnections = new ArrayList();
	private List enteringConnections = new ArrayList();
	
	@Override
	protected IFigure createFigure() {
		RouterWrapper wrapper = (RouterWrapper) this.getModel();
		
		IFigure figure = new RouterFigure(wrapper);
		return figure;
	}

	@Override
	protected void createEditPolicies() {

	}

	protected void refreshVisuals() {
		RouterWrapper wrapper = (RouterWrapper) this.getModel();

		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
				getFigure(), wrapper.calculateBounds());
		
		((CommonFigureInterface)this.getFigure()).refreshFigure();
	}

	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return new RouterAnchor(getFigure());
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return new RouterAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return new RouterAnchor(getFigure());
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return new RouterAnchor(getFigure());
	}

	protected List getModelSourceConnections() {
		NodeWrapper modelWrapper = (NodeWrapper) this.getModel();		
		leavingConnections.clear();
		leavingConnections.addAll(modelWrapper.getLeavingTransitions());
//		leavingConnections.addAll(modelWrapper.getLeavingLoops());

		return leavingConnections;
	}

	protected List getModelTargetConnections() {
		NodeWrapper modelWrapper = (NodeWrapper) this.getModel();
		enteringConnections.clear();
		this.enteringConnections.addAll(modelWrapper.getEnteringTransitions());
		return this.enteringConnections;
	}

	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(NodeWrapper.LOCATION)) {
			this.refreshVisuals();
		} else if (evt.getPropertyName()
				.equals(NodeWrapper.ENTERING_TRANSITION)||
				evt.getPropertyName().equals(NodeWrapper.ENTERING_LOOP))
			refreshTargetConnections();
		else if (evt.getPropertyName().equals(NodeWrapper.LEAVING_TRANSITION)||
				evt.getPropertyName().equals(NodeWrapper.LEAVING_LOOP))
			refreshSourceConnections();
	
	}

	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
		((AbsWrapper) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
		((AbsWrapper) getModel()).removePropertyChangeListener(this);
	}


}

